

GuildXpBarConfig = {}
GuildXpBar = {}

local GuildXpBarAddon = CreateFrame("Frame", "GuildXpBarAddon", UIParent)

local DefaultConfig = {}
local RegisteredEvents = {}
GuildXpBarAddon:SetScript("OnEvent", function (self, event, ...) if (RegisteredEvents[event]) then return RegisteredEvents[event](self, event, ...) end end)

local elap = 0


function RegisteredEvents:ADDON_LOADED(event, addon, ...)
	
	
	if (addon == "GuildXpBar") then
		self:UnregisterEvent("ADDON_LOADED")
		self.ADDON_LOADED = nil

		
		setmetatable(GuildXpBarConfig, {__index = DefaultConfig})

		GuildXpBarUI.CreateXPBar()
		GuildXpBarUI.CreateConfigMenu()
		print("GuildXpBar Loaded." )
		
		
		
		self:SetScript("OnUpdate", function(this, a1)
		elap = elap + a1
		if elap < 1 then 
			return 
		end
		
		GuildXpBarBar:Show()
		
		GuildXpBar.UpdateBar();
		--print(string.format("%.0f", (currentXP/ remainingXP *100 )) .."%")
			--print(currentXP)
		elap = 0
	end)
		--- comment next line out 
		--InterfaceOptionsFrame_OpenToCategory("GuildXpBar")
	end
end

for k, v in pairs(RegisteredEvents) do
	GuildXpBarAddon:RegisterEvent(k)
end

function GuildXpBar.UpdateBar()
	QueryGuildXP()
	local currentXP, remainingXP, dailyXP, maxDailyXP = UnitGetGuildXP("player"); 
	
	
	GuildXpBarBar:SetAlpha(GuildXpBar.GetConfigValue("barAlpha"))
	GuildXpBarBar:SetScale(GuildXpBar.GetConfigValue("barScale"))	
	GuildXpBarStatusBar:SetStatusBarColor(0.00,0.44, 0.87)		
	
	-- useing the var cuz i don't feel like renameing it
	if GuildXpBar.GetConfigValue("showAlways") then		
		GuildXpBarStatusBar:SetMinMaxValues(0,maxDailyXP);
		GuildXpBarStatusBar:SetValue(dailyXP);		
		GuildXpBarText:SetText(string.format("%.2f", dailyXP/maxDailyXP * 100).."%" );
				
	else
		
		GuildXpBarStatusBar:SetMinMaxValues(0,remainingXP+currentXP);
		GuildXpBarStatusBar:SetValue(currentXP);		
		GuildXpBarText:SetText(string.format("%.2f", currentXP/(currentXP + remainingXP) * 100).."%" );
	end
	--print(string.format("%.0f", currentXP/(currentXP + remainingXP) * 100) .. "%" )
	--print(string.format("%.0f", dailyXP/maxDailyXP * 100) .. "%" )
	
	--AbsorbBar.ShowOrHideBar()
end

function GuildXpBar.ShowOrHideBar()

--[[
	if GuildXpBar.GetConfigValue("showAlways") then
		--print("Set to always on")
		CreateGXBar:Show()
		return
	end
	
	if GuildXpBar.GetConfigValue("showStealth") then
		--if GuildXpBar.IsStealthed() then
		if IsStealthed() then
			--print("I am stealthed")
			CreateGXBar:Show()
			return
		else
			--print("I am not stealthed")
			--CreateGXBar:Hide()
		end
	end
	
	if GuildXpBar.GetConfigValue("showCombat") then
		if UnitAffectingCombat("player") then
			--print("I am in combat")
			CreateGXBar:Show()
			return
		else
			--print("I am not in combat")
			--CreateGXBar:Hide()
		end
	end
	--CreateGXBar:Hide()
	]]--
end

function GuildXpBar.SetLock(newValue)
	GuildXpBar.SetConfigValue("locked", newValue)
	if newValue then
		print("Energy Watch bar position locked")
	else
		print("Energy Watch bar position unlocked")
	end
end

function GuildXpBar.SlashCmdHandler(msg, editbox)
	--print("command is " .. msg .. "\n")
	if (string.lower(msg) == "config") then
		InterfaceOptionsFrame_OpenToCategory("GuildXpBar")
	elseif (string.lower(msg) == "dumpconfig") then
		print("With defaults")
		for k,v in pairs(DefaultConfig) do
			print(k,GuildXpBar.GetConfigValue(k))
		end
		print("Direct table")
		for k,v in pairs(GuildXpBarConfig) do
			print(k,v)
		end
	elseif (string.lower(msg) == "lock") then
		GuildXpBar.SetLock(true)
	elseif (string.lower(msg) == "unlock") then
		GuildXpBar.SetLock(false)
	elseif (string.lower(msg) == "reset") then
		GuildXpBar.SetConfigToDefaults()
	elseif (string.lower(msg) == "perf") then
		GuildXpBar.PrintPerformanceData()
	elseif (string.lower(msg) == "list") then
		-- used to find spell book id
		local i = 1
		while true do
			local spell = GetSpellBookItemName(i, BOOKTYPE_SPELL)
			if (not spell) then
				break
			end
		DEFAULT_CHAT_FRAME:AddMessage(i..": "..spell)
		i = i + 1
		end
	else
		--GuildXpBar.ShowHelp()
	end
end


function GuildXpBar.GetConfigValue(key)
	return GuildXpBarConfig[key]
end

function GuildXpBar.SetConfigValue(key, value)
	if (DefaultConfig[key] == value) then
		GuildXpBarConfig[key] = nil
	else 
		GuildXpBarConfig[key] = value
	end
end

function GuildXpBar.SetConfigToDefaults()
	print("Resetting config to defaults")
	GuildXpBarConfig = {}
	setmetatable(GuildXpBarConfig, {__index = DefaultConfig})

	CreateGXBar:ClearAllPoints()
	CreateGXBar:SetPoint('CENTER', UIParent)
	CreateGXBar:SetAlpha(GuildXpBar.GetConfigValue("barAlpha"))
	CreateGXBar:SetScale(GuildXpBar.GetConfigValue("barScale"))
	--GuildXpBar.ShowOrHideBar()
	GuildXpBarUI.CreateGXBar()
	
end



DefaultConfig = {
	showAlways = false,
	barWidth = 120,
	showCombat = true,
	barText = "&e/&em (&c)",
	barAlpha = 1.0,
	barScale = 1.0,
	locked = false
}
